﻿using shop.Bll;
using shop.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using unvell.ReoGrid;
using shop.Bll.Logi;
using CommonTools;
using CommonTools.Controls;

namespace shop.Logi
{
    public partial class LogisticsImportForm : DockContent
    {

        shop.Bll.Logi.LogisticsImportBll _bll = new LogisticsImportBll();


        public LogisticsImportForm()
        {
            InitializeComponent();

            this.box_begin.Value = DateTime.Today.AddMonths(-1);
            this.box_end.Value = DateTime.Today;
        }

        /// <summary> 导入 </summary>
        private async void btn_import_Click(object sender, EventArgs e)
        {
            var dt = ExcelHelper.Import();

            if (dt == null)
            {
                return;
            }

            var res = await WaitUI.RunAsync(async () =>
            {
                var list = dt.AsEnumerable()
                   .Select(a => new LogiTrace()
                   {
                       logistics_no = a.Field<string>(0),
                       LogisticsCompany = a.Field<string>(1),
                       DeliveryTime = a.Field<string>(2).IsNullDate(null),
                       LastUpdateTime = a.Field<string>(3).IsNullDate(null),
                       Timeliness = a.Field<string>(4),
                       SinceSent = a.Field<string>(5),
                       SinceLastUpdate = a.Field<string>(6),
                       ItemsCount = a.Field<string>(7).IsNull(0),
                       LastLogisticsUpdate = a.Field<string>(8),
                       LogisticsStatus = a.Field<string>(9),
                       QueryTime = a.Field<string>(10).IsNullDate(null),
                       FullLogisticsInfo = a.Field<string>(11),
                       TradeNo = a.Field<string>(12),
                       SecondLastLogisticsUpdate = a.Field<string>(13),
                       FirstLogisticsUpdate = a.Field<string>(14),
                       TaskRemarks = a.Field<string>(15),
                       isdel = false,
                   })
                   .ToList();

                return await _bll.SaveItems(list, UserBll.CurrentUser);
            });

           
            if (res.IsOk)
            {
                MessageTip.ShowOk(this, "导入完成");
            }
            else
            {
                MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {
            turnPageBox1_PageChanged(1, turnPageBox1.PageSize); //重新查询第一页
        }



        //快递单号	快递公司 揽收时间 最新时间	时效	发出至今	最新至今	条数	最后1条物流信息	物流状态	查询时间	完整物流信息	订单编号	倒数第2条物流信息	第1条物流信息	任务备注
        SheetColumn[] cols = new[]
        {
                     new SheetColumn(nameof(LogiTrace.logistics_no ), "快递单号"),
                   new SheetColumn(nameof(LogiTrace.LogisticsCompany ), "快递公司"),
                   new SheetColumn(nameof(LogiTrace.DeliveryTime ), "揽收时间"),
                   new SheetColumn(nameof(LogiTrace.LastUpdateTime ), "最新时间"),
                   new SheetColumn(nameof(LogiTrace.Timeliness ), "时效"),
                   new SheetColumn(nameof(LogiTrace.SinceSent ), "发出至今"),
                   new SheetColumn(nameof(LogiTrace.SinceLastUpdate ), "最新至今"),
                   new SheetColumn(nameof(LogiTrace.ItemsCount ), "条数"),
                   new SheetColumn(nameof(LogiTrace.LastLogisticsUpdate ), "最后1条物流信息"),
                   new SheetColumn(nameof(LogiTrace.LogisticsStatus ), "物流状态"),
                   new SheetColumn(nameof(LogiTrace.QueryTime ), "查询时间"),
                   new SheetColumn(nameof(LogiTrace.FullLogisticsInfo ), "完整物流信息"),
                   new SheetColumn(nameof(LogiTrace.TradeNo ), "订单编号"),
                   new SheetColumn(nameof(LogiTrace.SecondLastLogisticsUpdate ), "倒数第2条物流信息"),
                   new SheetColumn(nameof(LogiTrace.FirstLogisticsUpdate ), "第1条物流信息"),
                   new SheetColumn(nameof(LogiTrace.TaskRemarks ), "任务备注"),
            };


        private async void turnPageBox1_PageChanged(int index, int pageSize)
        {
            var begin = box_begin.Value.Value;
            var end = box_end.Value.Value;


            await WaitUI.RunAsync (async () =>
            {
                var res = await _bll.Query(begin, end, this.box_find.Text.Trim(), index, pageSize);

                if (res.IsOk)
                {
                    new GridDataBuilder(this.grid1)
                        .UseData(res.Data)
                        //.AutoCreateColumns()
                        .UseColumnIfEmpty(cols)
                        .UseFilter()
                        .End();

                    if (index == 1)
                    {
                        this.turnPageBox1.Reset(res.Rows);
                    }
                }
                else
                {
                    MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            });

        }
    }
}
